<template>
  <div>
    <ZgPage
      ref="page"
      title="系统数据源管理"
      addBtnName="新增数据源"
      :scrollX="2500"
      :addOpt="{title:'新增数据源'}"
      :updateOpt="{title:'修改数据源'}"
      :extraBtns="[{key:'test',icon:'fa-search-plus',name:'测试',isShow:()=>{return true}}]"
      @extraBtn="onExtraBtnClick"
      @search="onSearch"
      @del="onDel"
      @add="onAdd"
      @update="onUpdate"
      @batch="onBatch"
    >
      <template slot="columns">
        <a-table-column :width="150" dataIndex="code" title="编码"></a-table-column>
        <a-table-column :width="150" dataIndex="username" title="用户名"></a-table-column>
        <a-table-column :width="150" dataIndex="passwd" title="密码"></a-table-column>
        <a-table-column :width="250" dataIndex="driverClassName" title="驱动类"></a-table-column>
        <a-table-column dataIndex="jdbcUrl" title="连接串"></a-table-column>
      </template>
      <template slot="add">
        <Add ref="add"></Add>
      </template>
      <template slot="update" slot-scope="scope">
        <Update ref="update" :jsonStr="scope.jsonStr"></Update>
      </template>
      <template slot="batch" slot-scope="scope">
        <Batch ref="batch" :ids="scope.ids"></Batch>
      </template>
    </ZgPage>
  </div>
</template>
<script>
import Add from './Add'
import Update from './Update'
import Batch from './Batch'
export default {
  components: {
    Add, Update, Batch
  },
  data () {
    return {
    }
  },
  methods: {
    onSearch (param) {
      this.$http.get('/zg-system/datasource/page', param, data => {
        this.$refs.page.endSearch(
          {
            total: parseInt(data.total),
            list: data.records.map(t => {
              t.key = t.id
              return t
            })
          }
        )
      })
    },
    onDel (ids) {
      this.$http.post('/zg-system/datasource/del', ids, () => {
        this.$refs.page.search()
      })
    },
    onAdd () {
      const result = this.$refs.add.save()
      if (!result.isOk) {
        this.$error({ title: result.msg })
        return
      }
      this.$http.post('/zg-system/datasource/add', result.data, () => {
        this.$refs.page.hideAdd()
        this.$refs.page.search()
      })
    },
    onUpdate () {
      const result = this.$refs.update.save()
      if (!result.isOk) {
        this.$error({ title: result.msg })
        return
      }
      this.$http.post('/zg-system/datasource/update', result.data, () => {
        this.$refs.page.hideUpdate()
        this.$refs.page.search()
      })
    },
    onBatch () {
      const result = this.$refs.batch.save()
      if (!result.isOk) {
        this.$error({ title: result.msg })
        return
      }
      this.$http.post('/zg-system/datasource/batch', result.data, () => {
        this.$refs.page.hideBatch()
        this.$refs.page.search()
      })
    },
    onExtraBtnClick ({ key, obj }) {
      if (key === 'test') {
        this.$http.get('/zg-system/datasource/test', { key: obj.code }, data => {
          this.$info({ title: '连接成功，数据库名称：' + data.productName + '，数据库版本号：' + data.productVersion })
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
